const express = require('express');
const multer = require('multer');
const path = require('path');
const reportController = require('../controllers/reportController');

const router = express.Router();

// 配置 multer，用于文件上传
const storage = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, path.join(__dirname, '../../uploads')); // 文件存储目录
    },
    filename: (req, file, cb) => {
        const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9);
        cb(null, `${uniqueSuffix}-${file.originalname}`); // 确保文件名唯一
    },
});

const upload = multer({ storage });

// 上传报告并存储路径
router.post('/upload', upload.single('report'), reportController.uploadReport);

// 获取报告列表
router.get('/', reportController.getReports);

// 删除报告
router.delete('/:id', (req, res, next) => {
    console.log(`路由匹配成功: DELETE /api/reports/${req.params.id}`);
    next();
}, reportController.deleteReport);

// 查重功能
router.get('/check-duplicates', reportController.checkDuplicates);

// 摘要功能
router.post('/:id/summary', reportController.generateSummary.bind(reportController));

module.exports = router;
